﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Globalization;
using System.Linq;
using System.Reflection;
using System.Windows.Forms;
using Campus.Main.Basic.Settings;
using Campus.Properties;
using Campus.Report.ReportForm;
using Microsoft.Reporting.WinForms;
using dbcntn;
using Word = Microsoft.Office.Interop.Word;

namespace Campus.Main.Basic
{
    public partial class Kayit : Form
    {
        #region Fields
        protected List<string> Data = new List<string>();
        protected ConnectionToDb AToDb;
        protected DataSet ADataSet;
        protected string Donem;
        private int _prev;
        private int _next;
        private int _max;
        private int _min;
        private readonly string _id;
        private string _ort;
        public int pol;
        private HazirlikBall f;
        #endregion
        #region Constructor
        public Kayit()
        {
            InitializeComponent();
            txt_student_id.Leave += TextBox3Leave;
        }
        public Kayit(string id)
        {
            InitializeComponent();
            _id = id;
        }
        public Kayit(int k)
        {
            InitializeComponent();
        }
        #endregion
        #region Function

        public virtual int AddData()
        {
            Data.Clear();

            string studentNumber = txt_student_id.Text;
            string name = txt_name.Text;
            string pol = Convert.ToString(cmb_sex.SelectedValue);
            DateTime d=DateTime.Now;
            if (maskedTextBox1.Text.Contains('.'))
            {
                d=Helper.ChangeDateFormat1(maskedTextBox1.Text, '.');//dateTimePicker1.Value.ToString("yyyy-MM-dd",CultureInfo.InvariantCulture);
            }
            else if (maskedTextBox1.Text.Contains(','))
            {
                d = Helper.ChangeDateFormat1(maskedTextBox1.Text, ',');
            }
            string birthday = d.ToString("yyyy-MM-dd",CultureInfo.InvariantCulture);
            string birthPlace = txt_birth_place.Text;
            string homephone = txt_phone_number.Text;
            string homeaddress = txt_home_address.Text;
            string country = Convert.ToString(cmb_country.SelectedValue);
            string nation = Convert.ToString(cmb_nation.SelectedValue);
            string passport = txt_passport.Text;
            string bolum = Convert.ToString(cmb_bolum.SelectedValue);


            string handicap = Convert.ToString(cmb_student_handicap.SelectedValue);
            string ortalama = txt_ortalama.Text;
            string place = Convert.ToString(cmb_place.SelectedValue);
            string cource = Convert.ToString(cmb_cource.SelectedValue);
            string dhandicap = Convert.ToString(cmb_dad_handicap.SelectedValue);
            string mhandicap = Convert.ToString(cmb_mom_handicap.SelectedValue);
            string parentstate = Convert.ToString(cmb_parent_state.SelectedValue);
            string kardes = Convert.ToString(cmb_count.SelectedValue);
            


            string dname = txt_dad_name.Text;
            string dsurname = txt_dad_surname.Text;
            string dprofession = Convert.ToString(cmb_dad_profession.SelectedValue);
            string dworkaddress = txt_dad_address.Text;
            string dworkphone = txt_dad_phone.Text;
            string dincome = txt_dad_zarplata.Text;
            string dvalute = Convert.ToString(cmb_dad_valute.SelectedValue);
            string dhealth = Convert.ToString(cmb_dad_health.SelectedValue);
            


            string mname = txt_mom_name.Text;
            string msurname = txt_mom_surname.Text;
            string mprofession = Convert.ToString(cmb_mom_profession.SelectedValue);
            string mworkaddress = txt_mom_address.Text;
            string mworkphone = txt_mom_phone.Text;
            string mincome = txt_mom_zarplata.Text;
            string mvalute = Convert.ToString(cmb_mom_valute.SelectedValue);
            string mhealth = Convert.ToString(cmb_mom_health.SelectedValue);
            string aciklama = txt_aciklama.Text;
            string odul1 = Convert.ToString(comboBox1.SelectedValue);
            string hazirlikball_id = AToDb.SelectField("onmaintable", "hazirlikball_id", "student_id",
                                                       txt_student_id.Text);
            string puanortalama =AToDb.SelectField("SELECT id FROM puanortalama WHERE max_value>=" + ortalama + " and min_value<=" + ortalama);
            var tableName = new string[2, 9];
            int ball =Int32.Parse(AToDb.SelectField("select ifnull(ball,0) from onmaintable where student_id like('" + txt_student_id.Text +
                                  "')"));
            //if (txt_puan.Text != Convert.ToString(ball))
            {
                tableName[0, 0] = "cource";
                tableName[1, 0] = cource;
                tableName[0, 1] = "place";
                tableName[1, 1] = place;
                tableName[0, 2] = "parentstate";  
                tableName[1, 2] = parentstate;
                tableName[0, 3] = "studenthandicap";
                tableName[1, 3] = handicap;
                tableName[0, 4] = "parenthandicap";
                tableName[1, 4] = dhandicap;
                tableName[0, 5] = "parenthandicap";
                tableName[1, 5] = mhandicap;
                tableName[0, 6] = "kardes";
                tableName[1, 6] = kardes;
                tableName[0, 7] = "puanortalama";
                tableName[1, 7] = puanortalama;
                tableName[0, 8] = "hazirlik";
                tableName[1, 8] = hazirlikball_id;
                ball =AToDb.SelectTable(tableName);
            }
            int ceza = Int32.Parse(lbl_ceza.Text);
            string odul_old = AToDb.SelectField("onmaintable", "odul_id", "student_id", txt_student_id.Text);
            string odul = AToDb.SelectField("odul", "description", "id", Convert.ToString(comboBox1.SelectedValue)) ;
            /*if (odul_old != Convert.ToString(comboBox1.SelectedValue))
                odul = Convert.ToString(comboBox1.SelectedValue);
            else
                odul = AToDb.SelectField("odul", "description", "id", Convert.ToString(comboBox1.SelectedValue));*/
            
            //if (ceza != Int32.Parse(AToDb.SelectField("select ceza from onmaintable where student_id like('" + txt_student_id.Text + "')")))
            {
                ball += ceza;
            }
            ball += Int32.Parse(odul);
            if (Int32.Parse(cource)==1)
            {
                ball += f.Ball;
            }

            txt_puan.Text = Convert.ToString(ball);
            string date = DateTime.Now.ToString("yyyy-MM-dd",CultureInfo.InvariantCulture);//regdate 38
            string ID_student = txt_no.Text;
            string res = "0";
            if (chb_kkayit.Checked)
            {
                res = "1";
            }
            Data.Add(Donem);//Donem 1
            Data.Add(studentNumber);//Student_id 2
            Data.Add(name);//Name 3
            Data.Add(pol);//Pol 4
            Data.Add(birthday);//Birth_date 5 
            Data.Add(birthPlace);//Birth_place 6
            Data.Add(homephone);//HomePhone 7
            Data.Add(homeaddress);//HomeAddress 8
            Data.Add(country);//country_id 9
            Data.Add(nation);//nation_id 10
            Data.Add(passport);//passport 11
            Data.Add(handicap);//Handicap 12
            Data.Add(cource);//cource 13
            Data.Add(bolum);//bolum 14
            Data.Add(ortalama);//ortalama 15
            Data.Add(place);//place 16
            Data.Add(dname);//name 17
            Data.Add(dsurname);//surname 18
            Data.Add(dprofession);//profession 19
            Data.Add(dworkaddress);//workadres 20
            Data.Add(dworkphone);//workphone 21
            Data.Add(dincome);//income 22
            Data.Add(dvalute);//valute 23 
            Data.Add(dhandicap);//handicap 24
            Data.Add(dhealth);//health 25
            Data.Add(mname);//name 26 
            Data.Add(msurname);//surname 27
            Data.Add(mprofession);//profession 28
            Data.Add(mworkaddress);//workaddress 29
            Data.Add(mworkphone);//workphone 30
            Data.Add(mincome);//income 31
            Data.Add(mvalute);//valute 32
            Data.Add(mhandicap);//handicap 33
           Data.Add(mhealth);//health 34
            Data.Add(parentstate);//parentstate 35
            Data.Add(kardes);//kardes 36
            Data.Add(Convert.ToString(ball));//37 ball
            Data.Add(date);//38
            Data.Add(ID_student);//39
            Data.Add(res);//40
            Data.Add(Convert.ToString(ceza));//41
            Data.Add(aciklama);//42
            Data.Add(odul1);//43
            return ball;
           // MessageBox.Show(Resources.Kayit_AddData_+Convert.ToString(ball));
        }

        protected virtual void Initialize()
        {
            ClearAll();
            txt_student_id.Clear();
            lbl_ceza.Text = Resources.Kayit_Initialize__0;
            AToDb = new ConnectionToDb(Helper.ConnectionString);
            toolStripStatusLabel1.Text ="Onkayit olan ogrencilerin sayisi ="+ AToDb.SelectField("select count(*) from onmaintable where pol_id="+pol);
            AToDb.AddItem(cmb_nation, "nation");
            AToDb.AddItem(cmb_country, "country");
            AToDb.AddItem(cmb_place, "place");
            AToDb.AddItem(cmb_fakulty, "fakulty");
            AToDb.AddItem(cmb_dad_handicap, "parenthandicap");
            AToDb.AddItem(cmb_mom_handicap, "parenthandicap");
            AToDb.AddItem(cmb_dad_profession, "profession");
            AToDb.AddItem(cmb_dad_valute, "valute");
            AToDb.AddItem(cmb_mom_profession, "profession");
            AToDb.AddItem(cmb_mom_valute, "valute");
            AToDb.AddItem(cmb_sex, "pol");
            AToDb.AddItem(cmb_dad_health, "health");
            AToDb.AddItem(cmb_mom_health, "health");
            AToDb.AddItem(cmb_parent_state, "parentstate");
            AToDb.AddItem(cmb_student_handicap, "studenthandicap");
            AToDb.AddItem(cmb_count, "kardes");
            AToDb.AddItem(comboBox1, "odul");
            Donem = AToDb.SelectDate();//Helper.PrintConfig();

            txt_donem.Text = Donem;
            string temp = Convert.ToString(Convert.ToInt32(AToDb.SelectField("onmaintable", "id_student", "max")) + 1);
            txt_dosye_no.Text = temp;
            txt_no.Text = Convert.ToString(Convert.ToInt32(AToDb.SelectField("onmaintable", "no", "max","pol_id",pol)) + 1);
            _prev = Int32.Parse(temp) - 1;
            cmb_sex.SelectedValue = pol;
            cmb_sex.Enabled = false;
            chb_kkayit.Checked = false;
            chb_kkayit.Enabled = true;
            if (_prev==0)
            {
                button4.Enabled = false;
            }
            txt_puan.Text = "0";

        }
        protected virtual void ClearAll()
        {
            txt_dosye_no.Clear();
            //txt_donem.Clear();
            //txt_student_id.Clear();
            txt_name.Clear();
            txt_birth_place.Clear();
            txt_phone_number.Clear();
            txt_home_address.Clear();
            txt_passport.Clear();
            txt_ortalama.Clear();
            txt_ceza.Clear();
            txt_dad_name.Clear();
            txt_dad_surname.Clear();
            txt_dad_address.Clear();
            txt_dad_phone.Clear();
            txt_dad_zarplata.Clear();
            txt_mom_zarplata.Clear();
            txt_mom_phone.Clear();
            txt_mom_address.Clear();
            txt_mom_surname.Clear();
            txt_mom_name.Clear();
            maskedTextBox1.Clear();
            label18.Visible = false;
        }
        protected virtual void SetArxiv(string student, string tablename)
        {
            string id = AToDb.SelectField(tablename, "id_student", "student_id", student);
            pol = Int32.Parse(AToDb.SelectField(tablename, "pol_id", "student_id", student));
            toolStripStatusLabel1.Text = "Onkayit olan ogrencilerin sayisi =" + AToDb.SelectField("select count(*) from onmaintable where pol_id=" + pol);
            txt_dosye_no.Text = id;
            if (tablename == "arsiv")
            {
                txt_no.Text = Convert.ToString(Convert.ToInt32(AToDb.SelectField("onmaintable", "no", "max", "pol_id", pol)) + 1);
            }
            else
            {
                txt_no.Text = AToDb.SelectField(tablename, "no", "ID_student", id);
            }
            //txt_donem.Text = AToDb.SelectField(tablename, "donem", "ID_student", id);
            txt_donem.Text =AToDb.SelectDate();
            txt_name.Text = AToDb.SelectField(tablename, "name", "ID_student", id);
            txt_student_id.Text = AToDb.SelectField(tablename, "student_id", "ID_student", id);
            string birth = Helper.ChangeDateFormat(AToDb.SelectField(tablename, "birth_date", "ID_student", id)).ToString("dd.MM.yyyy", CultureInfo.InvariantCulture);
            maskedTextBox1.Text = birth;
            txt_puan.Text = AToDb.SelectField(tablename, "ball", "id_student", id);
            //dateTimePicker1.Text = AToDb.SelectField(tablename, "birth_date", "ID_student", id);
            if (tablename != "arsiv")
            {
                lbl_ceza.Text = AToDb.SelectField(tablename, "ceza", "ID_student", id);
            }
            txt_birth_place.Text = AToDb.SelectField(tablename, "birth_place", "id_student", id);
            txt_phone_number.Text = AToDb.SelectField(tablename, "home_telephone", "id_student", id);
            txt_home_address.Text = AToDb.SelectField(tablename, "home_address", "id_student", id);
            cmb_nation.SelectedValue = AToDb.SelectField(tablename, "nation_id", "id_student", id);
            cmb_country.SelectedValue = AToDb.SelectField(tablename, "country_id", "id_student", id);
            string bolum = AToDb.SelectField(tablename, "bolum_id", "id_student", id);
            cmb_fakulty.SelectedValue =
                AToDb.SelectField("select fakulty_id from bolum where id=" +
                                  bolum);
            cmb_bolum.SelectedValue = bolum; //aToDb.SelectField(tablename, "bolum_id", "id_student", id);
            cmb_cource.SelectedValue = AToDb.SelectField(tablename, "cource_id", "id_student", id);
            txt_passport.Text = AToDb.SelectField(tablename, "passport", "id_student", id);
            cmb_place.SelectedValue = AToDb.SelectField(tablename, "place_id", "id_student", id);
            cmb_sex.SelectedValue = pol = Int32.Parse(AToDb.SelectField(tablename, "pol_id", "id_student", id));

            txt_dad_name.Text = AToDb.SelectField(tablename, "dad_name", "id_student", id);

            txt_dad_surname.Text = AToDb.SelectField(tablename, "dad_surname", "id_student", id);
            cmb_dad_profession.SelectedValue = AToDb.SelectField(tablename, "dad_profession", "id_student", id);
            txt_dad_phone.Text = AToDb.SelectField(tablename, "dad_workphone", "id_student", id);
            txt_dad_address.Text = AToDb.SelectField(tablename, "dad_workaddress", "id_student", id);
            txt_dad_zarplata.Text = AToDb.SelectField(tablename, "dad_income", "id_student", id);
            cmb_dad_valute.SelectedValue = AToDb.SelectField(tablename, "valute_idd", "id_student", id);
            cmb_dad_health.SelectedValue = AToDb.SelectField(tablename, "dad_health_id", "id_student", id);
            cmb_dad_handicap.SelectedValue = AToDb.SelectField(tablename, "dad_handicap_id", "id_student", id);

            txt_mom_name.Text = AToDb.SelectField(tablename, "mom_name", "id_student", id);
            txt_mom_surname.Text = AToDb.SelectField(tablename, "mom_surname", "id_student", id);
            cmb_mom_profession.SelectedValue = AToDb.SelectField(tablename, "mom_profession", "id_student", id);
            txt_mom_phone.Text = AToDb.SelectField(tablename, "mom_workphone", "id_student", id);
            txt_mom_address.Text = AToDb.SelectField(tablename, "mom_workaddress", "id_student", id);
            txt_mom_zarplata.Text = AToDb.SelectField(tablename, "mom_income", "id_student", id);
            cmb_mom_valute.SelectedValue = AToDb.SelectField(tablename, "valute_idm", "id_student", id);
            cmb_mom_health.SelectedValue = AToDb.SelectField(tablename, "mom_health_id", "id_student", id);
            cmb_mom_handicap.SelectedValue = AToDb.SelectField(tablename, "mom_handicap_id", "id_student", id);

            cmb_parent_state.SelectedValue = AToDb.SelectField(tablename, "parent_state", "id_student", id);
            cmb_count.SelectedValue = AToDb.SelectField(tablename, "studyfamilycount", "id_student", id);
            cmb_student_handicap.SelectedValue = AToDb.SelectField(tablename, "handicap_id", "id_student", id);

            txt_ortalama.Text = AToDb.SelectField("EOgrOrtalamaReplYurt", "GOrt", "Numara", student);
            string res = AToDb.SelectField(tablename, "res", "id_student", id);
            if (res == "True")
            {
                chb_kkayit.Checked = true;
                chb_kkayit.Enabled = false;
            }
            else
            {
                chb_kkayit.Checked = false;
                chb_kkayit.Enabled = true;
            }
            button10.Enabled = true;
        }
        protected virtual void Set(string student,string tablename)
        {
            string id = AToDb.SelectField(tablename, "id_student", "student_id", student);
            pol = Int32.Parse(AToDb.SelectField(tablename, "pol_id", "student_id", student));
            toolStripStatusLabel1.Text ="Onkayit olan ogrencilerin sayisi ="+ AToDb.SelectField("select count(*) from onmaintable where pol_id="+pol);
            txt_dosye_no.Text = id;
            if (tablename == "arsiv")
            {
                txt_no.Text = Convert.ToString(Convert.ToInt32(AToDb.SelectField("onmaintable", "no", "max", "pol_id", pol)) + 1);
            }
            else
            {
                txt_no.Text = AToDb.SelectField(tablename, "no", "ID_student", id);
            }
            txt_donem.Text = AToDb.SelectField(tablename, "donem", "ID_student", id);
            txt_name.Text = AToDb.SelectField(tablename, "name", "ID_student", id);
            txt_student_id.Text = AToDb.SelectField(tablename, "student_id", "ID_student", id);
            string birth = Helper.ChangeDateFormat(AToDb.SelectField(tablename, "birth_date", "ID_student", id)).ToString("dd.MM.yyyy", CultureInfo.InvariantCulture);
            maskedTextBox1.Text=birth;
            txt_puan.Text = AToDb.SelectField(tablename, "ball", "id_student", id);
            //dateTimePicker1.Text = AToDb.SelectField(tablename, "birth_date", "ID_student", id);
            if (tablename != "arsiv")
            {
                lbl_ceza.Text = AToDb.SelectField(tablename, "ceza", "ID_student", id);
            }
            txt_birth_place.Text = AToDb.SelectField(tablename, "birth_place", "id_student", id);
            txt_phone_number.Text = AToDb.SelectField(tablename, "home_telephone", "id_student", id);
            txt_home_address.Text = AToDb.SelectField(tablename, "home_address", "id_student", id);
            cmb_nation.SelectedValue = AToDb.SelectField(tablename, "nation_id", "id_student", id);
            cmb_country.SelectedValue = AToDb.SelectField(tablename, "country_id", "id_student", id);
            string bolum = AToDb.SelectField(tablename, "bolum_id", "id_student", id);
            cmb_fakulty.SelectedValue =
                AToDb.SelectField("select fakulty_id from bolum where id=" +
                                  bolum);
            cmb_bolum.SelectedValue = bolum; //aToDb.SelectField(tablename, "bolum_id", "id_student", id);
            cmb_cource.SelectedValue = AToDb.SelectField(tablename, "cource_id", "id_student", id);
            txt_passport.Text = AToDb.SelectField(tablename, "passport", "id_student", id);
            cmb_place.SelectedValue = AToDb.SelectField(tablename, "place_id", "id_student", id);
            cmb_sex.SelectedValue= pol = Int32.Parse(AToDb.SelectField(tablename, "pol_id", "id_student", id));
            
            txt_dad_name.Text = AToDb.SelectField(tablename, "dad_name", "id_student", id);

            txt_dad_surname.Text = AToDb.SelectField(tablename, "dad_surname", "id_student", id);
            cmb_dad_profession.SelectedValue = AToDb.SelectField(tablename,"dad_profession", "id_student", id);
            txt_dad_phone.Text = AToDb.SelectField(tablename, "dad_workphone", "id_student", id);
            txt_dad_address.Text = AToDb.SelectField(tablename, "dad_workaddress", "id_student", id);
            txt_dad_zarplata.Text = AToDb.SelectField(tablename, "dad_income", "id_student", id);
            cmb_dad_valute.SelectedValue = AToDb.SelectField(tablename, "valute_idd", "id_student", id);
            cmb_dad_health.SelectedValue = AToDb.SelectField(tablename, "dad_health_id", "id_student", id);
            cmb_dad_handicap.SelectedValue = AToDb.SelectField(tablename, "dad_handicap_id", "id_student", id);

            txt_mom_name.Text = AToDb.SelectField(tablename, "mom_name", "id_student", id);
            txt_mom_surname.Text = AToDb.SelectField(tablename, "mom_surname", "id_student", id);
            cmb_mom_profession.SelectedValue = AToDb.SelectField(tablename, "mom_profession", "id_student", id);
            txt_mom_phone.Text = AToDb.SelectField(tablename, "mom_workphone", "id_student", id);
            txt_mom_address.Text = AToDb.SelectField(tablename, "mom_workaddress", "id_student", id);
            txt_mom_zarplata.Text = AToDb.SelectField(tablename, "mom_income", "id_student", id);
            cmb_mom_valute.SelectedValue = AToDb.SelectField(tablename, "valute_idm", "id_student", id);
            cmb_mom_health.SelectedValue = AToDb.SelectField(tablename, "mom_health_id", "id_student", id);
            cmb_mom_handicap.SelectedValue = AToDb.SelectField(tablename, "mom_handicap_id", "id_student", id);

            cmb_parent_state.SelectedValue = AToDb.SelectField(tablename, "parent_state", "id_student", id);
            cmb_count.SelectedValue = AToDb.SelectField(tablename, "studyfamilycount", "id_student", id);
            cmb_student_handicap.SelectedValue = AToDb.SelectField(tablename, "handicap_id", "id_student", id);

            comboBox1.SelectedValue = AToDb.SelectField(tablename, "odul_id", "id_student", id);
            txt_aciklama.Text = AToDb.SelectField(tablename, "odul_aciklama", "id_student", id);

            txt_ortalama.Text = AToDb.SelectField("EOgrOrtalamaReplYurt", "GOrt", "Numara", student);
            string res = AToDb.SelectField(tablename, "res", "id_student", id);
            if (res == "True")
            {
                chb_kkayit.Checked = true;
                chb_kkayit.Enabled = false;
            }
            else
            {
                chb_kkayit.Checked = false;
                chb_kkayit.Enabled = true;
            }
            button10.Enabled = false;
        }
        protected virtual void Set(int  id1,string tablename)
        {
            button10.Enabled = false;
            
            string id = Convert.ToString(id1);
            txt_dosye_no.Text = AToDb.SelectField(tablename, "ID_student", "ID_student", id);
            txt_no.Text = AToDb.SelectField(tablename, "no", "ID_student", id);
            txt_donem.Text = AToDb.SelectField(tablename, "donem", "ID_student", id);
            txt_name.Text = AToDb.SelectField(tablename, "name", "ID_student", id);
            txt_student_id.Text = AToDb.SelectField(tablename, "student_id", "ID_student", id);
            //DateTime d=Helper.ChangeDateFormat(AToDb.SelectField(tablename, "birth_date", "ID_student", id));
            string birth = Helper.ChangeDateFormat(AToDb.SelectField(tablename, "birth_date", "ID_student", id)).ToString("dd.MM.yyyy", CultureInfo.InvariantCulture);
            maskedTextBox1.Text = birth;
            txt_puan.Text = AToDb.SelectField(tablename, "ball", "ID_student", id);
            lbl_ceza.Text = AToDb.SelectField(tablename, "ceza", "ID_student", id);

            txt_birth_place.Text = AToDb.SelectField(tablename, "birth_place", "id_student", id);
            txt_phone_number.Text = AToDb.SelectField(tablename, "home_telephone", "id_student", id);
            txt_home_address.Text = AToDb.SelectField(tablename, "home_address", "id_student", id);
            cmb_nation.SelectedValue = AToDb.SelectField(tablename, "nation_id", "id_student", id);
            cmb_country.SelectedValue = AToDb.SelectField(tablename, "country_id", "id_student", id);
            string bolum = AToDb.SelectField(tablename, "bolum_id", "id_student", id);
            cmb_fakulty.SelectedValue =
                AToDb.SelectField("select fakulty_id from bolum where id=" +
                                  bolum);
            cmb_bolum.SelectedValue = bolum; //aToDb.SelectField(tablename, "bolum_id", "id_student", id);
            cmb_cource.SelectedValue = AToDb.SelectField(tablename, "cource_id", "id_student", id);
            txt_passport.Text = AToDb.SelectField(tablename, "passport", "id_student", id);
            cmb_place.SelectedValue = AToDb.SelectField(tablename, "place_id", "id_student", id);
            cmb_sex.SelectedValue = AToDb.SelectField(tablename, "pol_id", "id_student", id);
            txt_dad_name.Text = AToDb.SelectField(tablename, "dad_name", "id_student", id);

            txt_dad_surname.Text = AToDb.SelectField(tablename, "dad_surname", "id_student", id);
            cmb_dad_profession.SelectedValue = AToDb.SelectField(tablename, "dad_profession", "id_student", id);
            txt_dad_phone.Text = AToDb.SelectField(tablename, "dad_workphone", "id_student", id);
            txt_dad_address.Text = AToDb.SelectField(tablename, "dad_workaddress", "id_student", id);
            txt_dad_zarplata.Text = AToDb.SelectField(tablename, "dad_income", "id_student", id);
            cmb_dad_valute.SelectedValue = AToDb.SelectField(tablename, "valute_idd", "id_student", id);
            cmb_dad_health.SelectedValue = AToDb.SelectField(tablename, "dad_health_id", "id_student", id);
            cmb_dad_handicap.SelectedValue = AToDb.SelectField(tablename, "dad_handicap_id", "id_student", id);

            txt_mom_name.Text = AToDb.SelectField(tablename, "mom_name", "id_student", id);
            txt_mom_surname.Text = AToDb.SelectField(tablename, "mom_surname", "id_student", id);
            cmb_mom_profession.SelectedValue = AToDb.SelectField(tablename, "mom_profession", "id_student", id);
            txt_mom_phone.Text = AToDb.SelectField(tablename, "mom_workphone", "id_student", id);
            txt_mom_address.Text = AToDb.SelectField(tablename, "mom_workaddress", "id_student", id);
            txt_mom_zarplata.Text = AToDb.SelectField(tablename, "mom_income", "id_student", id);
            cmb_mom_valute.SelectedValue = AToDb.SelectField(tablename, "valute_idm", "id_student", id);
            cmb_mom_health.SelectedValue = AToDb.SelectField(tablename, "mom_health_id", "id_student", id);
            cmb_mom_handicap.SelectedValue = AToDb.SelectField(tablename, "mom_handicap_id", "id_student", id);

            cmb_parent_state.SelectedValue = AToDb.SelectField(tablename, "parent_state", "id_student", id);
            cmb_count.SelectedValue = AToDb.SelectField(tablename, "studyfamilycount", "id_student", id);
            cmb_student_handicap.SelectedValue = AToDb.SelectField(tablename, "handicap_id", "id_student", id);
            comboBox1.SelectedValue = AToDb.SelectField(tablename, "odul_id", "id_student", id);
            txt_aciklama.Text = AToDb.SelectField(tablename, "odul_aciklama", "id_student", id);

            txt_ortalama.Text = AToDb.SelectField(tablename, "oratalama", "id_student", id);
            string res = AToDb.SelectField(tablename, "res", "id_student", id) ;
            if (res=="True")
            {
                chb_kkayit.Checked = true;
                chb_kkayit.Enabled = false;
            }
            else
            {
                chb_kkayit.Checked = false;
                chb_kkayit.Enabled = true;
            }

        }
        protected virtual void SetBookmark(string[] bookmarkname, string[] value)
        {
            object oMissing = Missing.Value;
            Word._Application oWord = new Word.Application();
            object oTemplate = Application.StartupPath + "\\Template\\TemplateOK.dot";
            Word._Document oDoc = oWord.Documents.Add(ref oTemplate, ref oMissing, ref oMissing, ref oMissing);
            oWord.Visible = true;
            for (int i = 0; i < bookmarkname.Length; i++)
            {
                Object bookmarkNameObj = bookmarkname[i];
                Word.Range bookmarkRange = null;
                bookmarkRange = oDoc.Bookmarks.get_Item(ref bookmarkNameObj).Range;
                bookmarkRange.InsertAfter(value[i]);
            }

        }
        #endregion
        #region ComboBox
        private void ComboBox3SelectedValueChanged(object sender, EventArgs e)
        {
            Type t = typeof(Int32);
            if (cmb_fakulty.SelectedValue != null)
            {
                if (cmb_fakulty.SelectedValue.GetType() == t)
                {
                    AToDb.AddItem(cmb_bolum, "bolum", "fakulty_id", Convert.ToInt32(cmb_fakulty.SelectedValue));
                }
            }
        }
        private void ComboBox5SelectedIndexChanged(object sender, EventArgs e)
        {
            
        }

        private void ComboBox4SelectedIndexChanged(object sender, EventArgs e)
        {
            AToDb.AddItem(cmb_cource,"cource");
        }
        private void ComboBox6SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cmb_place.SelectedIndex+1>0)
            {
                button1.Enabled = true;
            }
        }
        private void ComboBox1SelectedValueChanged(object sender, EventArgs e)
        {
            
        }
        private void ComboBox2SelectedValueChanged(object sender, EventArgs e)
        {
           
        }
        #endregion
        #region Button

        protected virtual void button2_Click(object sender, EventArgs e)
        {
            if (txt_student_id.Text!="")
            {
                int new_ceza = txt_ceza.Text=="" ? 0 : Int32.Parse(txt_ceza.Text);
                int old_ceza = Int32.Parse(lbl_ceza.Text);
                old_ceza -= new_ceza;
                lbl_ceza.Text = Convert.ToString(old_ceza);
                int puan=Int32.Parse(txt_puan.Text);
                puan -= new_ceza;
                txt_puan.Text = Convert.ToString(puan);
                AToDb.UpdateMainTable("ball",puan,txt_student_id.Text);
                AToDb.UpdateMainTable("ceza",old_ceza,txt_student_id.Text);
                txt_ceza.Clear();
            }
        }

        private void FormClose(object sender, FormClosedEventArgs e)
        {
            lbl_ceza.Text = AToDb.SelectField("select sum(a.ball) from ceza a,disiplin b where a.ID=b.ceza and b.student_id like('" + txt_student_id.Text + "')");
            txt_puan.Text = AToDb.SelectField("select ball from onmaintable where student_id like('" + txt_student_id.Text + "') ");
        }

        private void Button1Click(object sender, EventArgs e)
        {
            if (Convert.ToInt32(cmb_cource.SelectedValue)==1&&txt_student_id.Text.Length>0)
            {
                if (AToDb.CheckDataFromMainTable(txt_student_id.Text))
                {
                    f = new HazirlikBall(txt_student_id.Text);
                    f.FormClosing += child_close;
                    f.ShowDialog();
                }
                else
                {
                    MessageBox.Show("Bu ogrenci kayit olmamistir");
                }
            }
            else
            {
                MessageBox.Show("Ogrenci numarasi veya sinifi belirlenmemistir");
            }
        }

        private void child_close(object sender, FormClosingEventArgs e)
        {
            Button9Click(null,null);
        }

        private void Button2Click(object sender, EventArgs e)
        {
            var f=new Ceza(txt_student_id.Text);
            f.FormClosing += Bind;
            f.ShowDialog();
        }

        protected void Bind(object sender, FormClosingEventArgs e)
        {
            TxtOrtalamaTextChanged(null,null);
        }

        protected void Button11Click(object sender, EventArgs e)
        {
            Close();
        }

        protected virtual void Button7Click(object sender, EventArgs e)
        {
            ClearAll();
            Initialize();
            button10.Enabled = true;
            button4.Enabled = true;
            txt_student_id.Leave += TextBox3Leave;
        }

        protected virtual void Button10Click(object sender, EventArgs e)
        {
            int ball = AddData();
            MessageBox.Show("Toplam puan=" + Convert.ToString(ball));
            AToDb.InsertIntoMainTable(Data);
            MessageBox.Show("Kayit Eklendi");
            //Initialize();
        }

        protected virtual void Button9Click(object sender, EventArgs e)
        {
            //Update
            if (Int32.Parse(txt_dosye_no.Text) <= Int32.Parse(AToDb.SelectField("select max(ID_student) from onmaintable")))
            {
                AddData();
                AToDb.UpdateMainTable(Data, txt_dosye_no.Text);
                MessageBox.Show("Duzenlendi");
            }
        }

        protected virtual  void Button8Click(object sender, EventArgs e)
        {
            //Delete
            int no = Int32.Parse(txt_no.Text);
            if (MessageBox.Show("Kayiti silmek istiyourmusunuz???","Dikkat",MessageBoxButtons.YesNo,MessageBoxIcon.Exclamation,MessageBoxDefaultButton.Button2)==DialogResult.Yes)
            {
                AToDb.DeleteTable("onmaintable","student_id",txt_student_id.Text);
                AToDb.Call("call autodecok("+no+","+pol+")");
                Button7Click(null,null);
                Initialize();
            }
        }

        protected virtual void Button3Click(object sender, EventArgs e)
        {
            //Min(ID)
            _min = Int32.Parse(AToDb.SelectField("select min(id_student) from onmaintable where pol_id=" + pol));
            Set(_min, "onmaintable");
            button4.Enabled = false;
            button5.Enabled = true;
            button10.Enabled = false;
        }

        protected virtual void Button6Click(object sender, EventArgs e)
        {
            //Max(ID)
            _max = Int32.Parse(AToDb.SelectField("select max(id_student) from onmaintable where pol_id=" + pol));
            Set(_max, "onmaintable");
            button5.Enabled = false;
            button4.Enabled = true;
            button10.Enabled = false;
        }

        protected virtual  void Button4Click(object sender, EventArgs e)
        {
            //Prev
            _prev = Int32.Parse(AToDb.SelectField("select id_student from onmaintable where no=" + txt_no.Text + "-1 and pol_id=" + pol));
            if (_prev != 0)
            {
                if (_next <= Int32.Parse(AToDb.SelectField("select max(id_student) from onmaintable where pol_id="+pol) + 1))
                {
                    button5.Enabled = true;
                }
                Set(_prev,"onmaintable");
            }
            else
            {
                button4.Enabled = false;
            }
        }

        protected virtual void Button5Click(object sender, EventArgs e)
        {
            //Next
            _next = Int32.Parse(AToDb.SelectField("select id_student from onmaintable where no=" + txt_no.Text + "+1 and pol_id=" + pol));
            if (_next!=0)
            {
                if (_next <= Int32.Parse(AToDb.SelectField("select max(id_student) from onmaintable where pol_id="+pol)))
                {
                    Set(_next, "onmaintable");
                }
                if (_prev!=1)
                {
                    button4.Enabled = true;
                }
            }
            else
            {
                button5.Enabled = false;
            }
        }
        #endregion
        #region TextBox
        private void TextBoxKeyPress(object sender, KeyPressEventArgs e)
        {
            e.Handled = (e.KeyChar < 48 || e.KeyChar > 57) && e.KeyChar != 8 && e.KeyChar != 'Y';
        }

        protected virtual void TextBox3TextChanged(object sender, EventArgs e)
        {

        }

        protected void TextBox3KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar=='.')
            {
                if ((txt_student_id.Text.Trim().Contains(".")&&txt_student_id.Text.IndexOf('.')==4))
                {
                    e.Handled = true;
                }
                else
                {
                    e.Handled = false;
                }
            }
        }

        private void TextBox16KeyPress(object sender, KeyPressEventArgs e)
        {
            if ((e.KeyChar<48||e.KeyChar>57)&&e.KeyChar!=8)
            {
                e.Handled = true;
            }
            else
            {
                e.Handled = false;
            }
        }

        protected virtual void TextBox3Leave(object sender, EventArgs e)
        {
            ClearAll();
            if (txt_student_id.Text.Length > 0 || txt_student_id.Text != "")
            {
                string t =
                        AToDb.SelectField("select id_student from onmaintable where student_id='" + txt_student_id.Text +
                                          "'");
                if (t != "0")
                {
                    MessageBox.Show("Бул студент каттоодон откон");
                    ClearAll();
                    Initialize();
                    txt_student_id.Focus();
                    return;
                }
                if (AToDb.CheckDataFromArxiv(txt_student_id.Text))
                {
                    MessageBox.Show("True");
                    if (AToDb.CheckDataFromArxiv(txt_student_id.Text, "kkayit", "1"))
                    {
                        MessageBox.Show("True2");
                        if (
                            MessageBox.Show("Kayit Kesin Silinmis. Kayit Yapmak Istiyormusunuz????", "Dikkat",
                                            MessageBoxButtons.YesNo) == DialogResult.Yes)
                        {
                            Set(txt_student_id.Text, "arsiv");
                            var ceza =
                        AToDb.SelectField(
                            "select sum(a.ball) from ceza a,disiplin b where a.ID=b.ceza and b.student_id like ('" +
                            txt_student_id.Text + "')");
                            lbl_ceza.Text = ceza;
                        }
                        else
                        {
                            txt_student_id.Clear();
                            txt_student_id.Focus();
                        }
                    }
                    else
                    {
                        SetArxiv(txt_student_id.Text, "arsiv");
                        if (comboBox1.SelectedValue==null)
                        {
                            comboBox1.SelectedIndex = 0;
                        }
                        var ceza =
                        AToDb.SelectField(
                            "select sum(a.ball) from ceza a,disiplin b where a.ID=b.ceza and b.student_id like ('" +
                            txt_student_id.Text + "')");
                        lbl_ceza.Text = ceza;
                    }
                }
                else
                {
                    _ort = AToDb.SelectOrtalama(txt_student_id.Text);
                    var name = AToDb.SelectTable("EOgrOrtalamaReplYurt", "numara", "adsoyad", txt_student_id.Text);
                    var ceza =
                        AToDb.SelectField(
                            "select sum(a.ball) from ceza a,disiplin b where a.ID=b.ceza and b.student_id like ('" +
                            txt_student_id.Text + "')");
                    lbl_ceza.Text = ceza;
                    _ort = _ort.Replace(',', '.');
                    txt_name.Text = name;
                    if (_ort == "")
                    {
                        _ort = "0";
                    }
                    txt_ortalama.Text = _ort;
                }
            }
        }
        protected virtual void TxtOrtalamaTextChanged(object sender, EventArgs e)
        {
            if (txt_ortalama.Text.Length > 0)
            {
                if (Convert.ToDouble(txt_ortalama.Text) > 2.5)
                {
                    label18.Text = Resources.Kayit_Button1Click_Burs_aliyour;
                    label18.ForeColor = Color.Chartreuse;
                }
                else if (Convert.ToString(cmb_cource.SelectedValue) == "1" && Convert.ToDouble(txt_ortalama.Text) == 0)
                {
                    label18.Text = Resources.Kayit_Button1Click_Burs_aliyour;
                    label18.ForeColor = Color.Chartreuse;
                }
                else
                {
                    label18.Text = Resources.Kayit_Button1Click_Burs_almiyour;
                    label18.ForeColor = Color.Crimson;
                }
                label18.Visible = true;
            }
            else
            {
                txt_ortalama.Text = Resources.Kayit_TxtOrtalamaTextChanged__0;
            }
        }
        #endregion
        #region Form
        private void KayitLoad(object sender, EventArgs e)
        {
            Initialize();
            if (_id!=null)
                Set(_id,"onmaintable");
            
        }
        #endregion
        #region Menu
        private void HakkindaToolStripMenuItemClick(object sender, EventArgs e)
        {
            var about = new About {StartPosition = FormStartPosition.CenterScreen};
            about.ShowDialog();
        }
        

        private void IlkKayitToolStripMenuItemClick(object sender, EventArgs e)
        {
            Button3Click(null,null);
        }

        private void SonrakiKayitToolStripMenuItemClick(object sender, EventArgs e)
        {
            Button5Click(null,null);
        }

        private void OncekiKayitToolStripMenuItemClick(object sender, EventArgs e)
        {
            Button4Click(null, null);
        }

        private void SonkiKayitToolStripMenuItemClick(object sender, EventArgs e)
        {
            Button6Click(null, null);
        }

        private void YeniKayitToolStripMenuItemClick(object sender, EventArgs e)
        {
            Button7Click(null, null);
        }

        private void KayitSilToolStripMenuItemClick(object sender, EventArgs e)
        {
            Button8Click(null, null);
        }

        private void KayitDuzenleToolStripMenuItemClick(object sender, EventArgs e)
        {
            Button9Click(null, null);
        }

        private void KaydetToolStripMenuItemClick(object sender, EventArgs e)
        {

        }

        private void IptalEtToolStripMenuItemClick(object sender, EventArgs e)
        {
            Button11Click(null, null);
        }

        protected virtual void AramaFormuToolStripMenuItemClick(object sender, EventArgs e)
        {
            var a=new SearchForm();
            a.FormClosed += KayitFormClosed;
            Hide();
            a.ShowDialog();
        }

        private void OnkayitFormuToolStripMenuItemClick(object sender, EventArgs e)
        {
            if (txt_student_id.Text != null && txt_name.Text != "")
            {
                string bolum =
                    AToDb.SelectField("select description from bolum where id=" +
                                      Convert.ToString(cmb_bolum.SelectedValue));
                string fakulty =
                    AToDb.SelectField("select b.description from bolum a,fakulty b where a.fakulty_id=b.id and a.id=" +
                                      Convert.ToString(cmb_bolum.SelectedValue));
                string cource =
                    AToDb.SelectField("select description from cource where id=" +
                                      Convert.ToString(cmb_cource.SelectedValue));
                string country =
                    AToDb.SelectField("select description from country where id=" +
                                      Convert.ToString(cmb_country.SelectedValue));
                string nation =
                    AToDb.SelectField("select description from nation where id=" +
                                      Convert.ToString(cmb_nation.SelectedValue));
                string h1 =
                    AToDb.SelectField(
                        "select a.description from health a,onmaintable b where b.dad_health_id=a.id and a.id=" +
                        Convert.ToString(cmb_dad_health.SelectedValue));
                string h2 =
                    AToDb.SelectField(
                        "select a.description from health a,onmaintable b where b.mom_health_id=a.id and a.id=" +
                        Convert.ToString(cmb_mom_health.SelectedValue));
                string i1 = txt_dad_zarplata.Text;
                string i2 = txt_mom_zarplata.Text;
                string address = txt_home_address.Text;
                string kardes =
                    AToDb.SelectField(
                        "select a.description from kardes a,onmaintable b where b.studyfamilycount=a.id and a.id=" +
                        Convert.ToString(cmb_count.SelectedValue));
                string regdate = AToDb.SelectField("onmaintable", "cast(date(regdate) as char)", "student_id",
                                                   txt_student_id.Text);
                string ID_student = txt_no.Text;

                string[] bk =
                    {
                        "ID_student", "regdate", "student_id", "name", "birth_place", "birth_date", "fakulty",
                        "bolum", "cource", "country", "nation", "dhealth", "mhealth", "dincome", "mincome",
                        "address", "kardes"
                    };
                string[] val =
                    {
                        ID_student, regdate, txt_student_id.Text, txt_name.Text, txt_birth_place.Text,
                        maskedTextBox1.Text, fakulty, bolum, cource, country, nation, h1, h2, i1, i2, address, kardes
                    };
                List<ReportParameter> par = bk.Select((t, i) => new ReportParameter(t, val[i])).ToList();
                ReportForm f = new ReportForm();
                f.reportViewer1.ProcessingMode = ProcessingMode.Local;
                f.reportViewer1.LocalReport.ReportPath = "ReportTemplate/OKayitReport.rdlc";
                f.reportViewer1.LocalReport.SetParameters(par);
                f.ShowDialog();
            }
        }
        #endregion

        protected void KayitFormClosed(object sender, FormClosedEventArgs e)
        {
            Close();
        }

        private void button12_Click(object sender, EventArgs e)
        {

        }

        private void lbl_ceza_TextChanged(object sender, EventArgs e)
        {

        }

        private void maskedTextBox1_Leave(object sender, EventArgs e)
        {
            string s = maskedTextBox1.Text;
            s = s.Substring(6);
            if (s[0] == '9')
            {
                s = "19" + s;
            }
            if (s[0] == '0')
            {
                s = "20" + s;
            }
            string temp = maskedTextBox1.Text;
            temp = temp.Remove(6);
            temp += s;
            maskedTextBox1.Text = temp;
        }

        private void button12_Click_1(object sender, EventArgs e)
        {
            if (MessageBox.Show("Ogrenim yilin yenilemek istiormusunuz???", "Dikkat", MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
                //Helper.DeleteDate(0);
                AToDb.UpdateDate(txt_donem.Text);
                AToDb.Call("Truncate table onmaintable");
                AToDb.DeleteTable("kmaintable");
                AToDb.Call("Truncate table kmaintable");
                if (MessageBox.Show("Ogrenim yilin yenilemek istiormusunuz???", "Dikkat", MessageBoxButtons.OK) == DialogResult.OK)
                    Initialize();
            }
        }

        private void yardimToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Process.Start(Application.StartupPath + "\\Help.docx");
        }
       
       
    }
}
   
